package hello;

import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;
import javax.microedition.midlet.MIDlet;
import javax.microedition.midlet.MIDletStateChangeException;

import video.MIDPPixelSource;
import video.VideoListener;
import connections.FilePoster;

public class HelloVideoSender extends MIDlet {

	public VideoCanvas myCanvas;

	public MIDPPixelSource ps;

	public void startApp() throws MIDletStateChangeException {

		myCanvas = new VideoCanvas();
		ps = new MIDPPixelSource(160, 120, null, myCanvas);
		Display display = Display.getDisplay(this);
		display.setCurrent(myCanvas);
		// ps = new MIDPPixelSource(160, 120,"http://java.sun.com/products/java-media/mma/media/test-mpeg.mpg", myCanvas);
		// ps = new MIDPPixelSource(160, 120,"http://itp.nyu.edu/~dbo3/cellphone/testvid/h.MPG");
		
		// ps.addVideoListener(myCanvas, 15);

	}

	public void pauseApp() {
	}

	public void destroyApp(boolean arg0) throws MIDletStateChangeException {
		System.out.println("destroy");
	}

	public class VideoCanvas extends Canvas {

		int x;

		int y;

		// Image myImage;
		StringBuffer feedback = new StringBuffer();

		public void keyPressed(int keyCode) {
			System.out.println(keyCode);

			System.out.println("Keycode" + keyCode);

			if (keyCode == Canvas.KEY_NUM4  || keyCode == 37) {
				x--;
			} else if (keyCode == Canvas.KEY_NUM6  || keyCode == 39) {
				x++;

			} else if (keyCode == Canvas.KEY_NUM2  || keyCode == 38) {
				y--;
			} else if (keyCode == Canvas.KEY_NUM8  || keyCode == 40) {
				y++;
			} else if (keyCode == Canvas.KEY_NUM5  || keyCode == 10) {
				// grab
				ps.grabFrame();
				try {
					byte[] imageBytes = ps.getCompressedImageBytes();
					String extraInfo = "DanoPicture" + "," + x + "," + y + "," + String.valueOf(System.currentTimeMillis()) + ".png"; // use a timestamp for the filename on the server
					// send it
					new FilePoster(imageBytes, "http://itp.nyu.edu/~dbo3/up.php", extraInfo, feedback);

				} catch (Exception e) {
					put("Something bad happened in send");
				}

			} else if (keyCode == Canvas.KEY_NUM0) {
				// thanks to stefan

				try {
					destroyApp(true);
				} catch (MIDletStateChangeException e) {
					e.printStackTrace();
				}

				notifyDestroyed();
			}

			repaint();
			ps.setLocationOnCanvas(x, y);

		}



		public void put(String _s) {
			System.out.println(_s);
			feedback.delete(0, feedback.length());
			feedback.append(_s);
		}

		public void paint(Graphics g) {
	
			g.setColor(255, 255, 255);
			g.fillRect(0, 0, getWidth(), getHeight());
			if (ps.requiresManualPainting()){
				ps.grabFrame();
				Image snapshot = ps.getImage();
				g.drawImage(snapshot, x, y, 0);
			}

			g.setColor(0);
			g.drawString(feedback.toString(), 10, 140, 0);
	
		}

	}

	
}
